home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) Silicon Graphics, Inc. 1996 */
-
- /* panZoom.c
- * This program takes the name of an image file in SGI .rgb
- * format. It opens the file and creates a window displaying
- * a subimage taken from the middle of the image.
- *
- * The image can be panned by dragging the mouse with the
- * Left Mousebutton pressed.
- *
- * The subimage can be zoomed in or out using the middle and
- * right mouse buttons, respectively.
- *
- * Note that this program must be linked with -lrgbImageFile,
- * which should be in the ../lib directory.
- */
-
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "rgbImageFile.h" /* should be in ../../include */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
- GLvoid motion( GLint, GLint );
-
- GLvoid adjustSkipParams( GLvoid );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- GLuint *image;
- GLint skipRows, skipPixels;
- GLint xStart, yStart;
-
- GLint imageWidth, imageHeight;
- GLint subimageWidth, subimageHeight;
-
- GLboolean panning = GL_FALSE;
-
- GLfloat zoomFactor = 1.0;
-
- GLvoid
- main ( int argc, char *argv[])
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- if (argc < 2) {
- fprintf (stderr, "usage: %s <imageFileName>\n",
- argv[0] );
- exit (1);
- }
-
- image = rgbReadImageFile(argv[1], &imageWidth, &imageHeight);
-
- /* set up the window size and the sub-image size
- * to be 1/4 of the original image size.
- */
- subimageWidth = imageWidth/2;
- subimageHeight = imageHeight/2;
-
- glutInitWindowPosition( 100, 100 );
- glutInitWindowSize( subimageWidth, subimageHeight );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutMouseFunc( mouse );
- glutMotionFunc( motion );
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout,
- "\n%s, a program to pan and Zoom an image\n\n"\
- "Left Mousebutton, down - pan image\n\n"
- "<z> key - zoom in on subimage\n"
- "<Z> key - zoom out on subimage\n"
- "Escape key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- initgfx( void )
- {
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'z': /* increase zoom factor -- zoom in */
- zoomFactor *= 2;
- break;
- case 'Z': /* decrease zoom factor -- zoom out */
- zoomFactor /= 2;
- break;
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- printf("Zoom Factor = %g\n", zoomFactor);
- glutPostRedisplay();
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- glViewport( 0, 0, width, height);
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluOrtho2D( 0.0, (GLdouble) width, 0.0, (GLdouble) height );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.375, 0.375, 0.0 );
-
- subimageWidth = width;
- subimageHeight = height;
- if (subimageWidth > imageWidth)
- subimageWidth = imageWidth;
- if (subimageHeight > imageHeight)
- subimageHeight = imageHeight;
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- if (state == GLUT_DOWN) {
- xStart = x; /* save current mouse position */
- yStart = y;
- panning = GL_TRUE;
- } else {
- panning = GL_FALSE;
- }
- break;
- }
- }
-
- GLvoid
- motion( int x, int y )
- {
- if (panning) {
- /* adjust skip info based on how far the mouse moved */
- skipPixels -= (x - xStart);
- skipRows += (y - yStart);
-
-
- xStart = x; /* Update the stored mouse position */
- yStart = y;
-
- glutPostRedisplay();
- }
- }
-
- /* make sure the selected rectangle is always
- * completely inside the original image
- */
- GLvoid
- adjustSkipParams( GLvoid )
- {
- if (skipPixels < 0)
- skipPixels = 0;
- if (skipPixels > imageWidth-subimageWidth)
- skipPixels = imageWidth-subimageWidth;
-
- if (skipRows < 0)
- skipRows = 0;
- if (skipRows >= imageHeight-subimageHeight)
- skipRows = imageHeight-subimageHeight;
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- glClear( GL_COLOR_BUFFER_BIT );
-
- adjustSkipParams ();
-
- /* Set up to grab a portion of the original stored image. */
-
- /* Set the pixel width of the original image. */
- glPixelStorei( GL_UNPACK_ROW_LENGTH, imageWidth );
-
- /* Indicate how many pixels to skip in the x and y directions
- * (from the lower left corner of the original image) */
- glPixelStorei( GL_UNPACK_SKIP_PIXELS, skipPixels );
- glPixelStorei( GL_UNPACK_SKIP_ROWS, skipRows );
-
- /* In this example, the xy coordinates to map directly
- * to window coordinates. This would not be the case if any
- * projection or modeling transforms had been applied.
- */
- glRasterPos2i( 0, 0 );
- glPixelZoom( zoomFactor, zoomFactor );
- glDrawPixels (subimageWidth, subimageHeight, GL_RGBA,
- GL_UNSIGNED_BYTE, image);
-
- glutSwapBuffers();
- }
-